﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Windows.Forms;
using System.Data.Odbc;
using Npgsql;
using System.Data;

namespace BibliotecaClasse
{
    public class ConexaoPostgres
    {

        private String serverName = "127.0.0.1";
        private String port = "5432";
        private String userName = "postgres";
        private String password = "postgres";
        private String dbName = "pcs";
        private String connString = null;
        private static ConexaoPostgres instancia;

        /**
         * implementa singleton
         */ 
        public static ConexaoPostgres getInstancia()
        {
            if (instancia == null)
            {
                instancia = new ConexaoPostgres();
            }
            return instancia;
        }


        private ConexaoPostgres()
        {
            connString = String.Format("Server={0};Port={1};User Id={2};Password={3};Database={4};",
                                        serverName,port,userName,password,dbName);
            
        }

        

        

        public NpgsqlConnection conectar()
        {
            NpgsqlConnection con = null;
            try
            {
                con = new NpgsqlConnection(connString);
                //OdbcConnection con = new OdbcConnection("DSN=PostgreSQL30");
                //con.Open();
                ////Conn = new SqlConnection(strCon);
                ////Conn.Open();
                //MessageBox.Show("aeeeeeeeeeeeeeee!!!");
                
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
            return con;
        }
        
        public void desconectar(NpgsqlConnection con)
        {
            try
            {
                //OdbcConnection con = new OdbcConnection("DSN=PostgreSQL30");
                //con.Close(); 
                con.Close();
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
        }
    }
}
